/*
 * Melomano - simple music player for Android
 *
 * Copyright (C) 2011  Andriy Tsykholyas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tsyko.android.melomano.view;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tsyko.android.melomano.R;

import java.io.File;
import java.util.List;


public class FileListAdapter extends ArrayAdapter<FileListItem> {

    private static final Logger logger = LoggerFactory.getLogger(FileListAdapter.class);

    private final FolderViewActivity parentActivity;


    public FileListAdapter(FolderViewActivity parentActivity, List<FileListItem> items) {
        super(parentActivity, R.layout.file_list_item, items);
        this.parentActivity = parentActivity;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;

//        logger.debug("getView() position {}, convertView {}", position, convertView);

        if (convertView == null) {
            ViewHolder viewHolder = new ViewHolder();
            LayoutInflater inflator = parentActivity.getLayoutInflater();
            view = inflator.inflate(R.layout.file_list_item, null);
            viewHolder.fileNameView = (TextView) view.findViewById(R.id.file_name);
            view.setTag(viewHolder);

            view.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    processOnClick(position);
                }
            });

            view.setOnLongClickListener(new View.OnLongClickListener() {
                public boolean onLongClick(View v) {
                    processOnLongClick(position);
                    return true;
                }
            });
        } else {
            view = convertView;
        }
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        viewHolder.fileNameView.setText(getItem(position).getFile().getName());

        return view;
    }


    private void processOnClick(int position) {
        logger.debug("processOnClick() {}", position);

        final File file = getItem(position).getFile();
        if (file.isDirectory()) {
            parentActivity.setCurrentDirectory(file);
        } else {
            // TODO
        }
    }

    private void processOnLongClick(int position) {
        logger.debug("processOnLongClick() {}", position);

        // TODO
    }


    private static class ViewHolder {
        TextView fileNameView;
    }
}
